package com.lijian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lijian.mapper.AdminMapper;
import com.lijian.mapper.UserMapper;
import com.lijian.pojo.Admin;
import com.lijian.pojo.User;
import com.lijian.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/account")
@RestController
public class AccountController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AdminMapper adminMapper;

    @PostMapping("/login")
    public Object login(@RequestParam String type, @RequestParam String username, @RequestParam String password) {
        Object object = null;
        switch (type) {
            case "user":
                QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
                userQueryWrapper.eq("username", username);
                userQueryWrapper.eq("password", MD5Utils.code(password));
                object = userMapper.selectOne(userQueryWrapper);
                break;
            case "admin":
                QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
                adminQueryWrapper.eq("username", username);
                adminQueryWrapper.eq("password", MD5Utils.code(password));
                object = adminMapper.selectOne(adminQueryWrapper);
                break;
        }
       /* if (object == null) {
            throw new RuntimeException("用户名或密码错误");
        }*/
        return object;
    }
}
